package com.example.demo.utils;

import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Map;

@Slf4j
public class PdfUtils {
    void testPdf(Map<String, Object> hashmap) {
        String inputFileName = "D:\\Demos\\system_back\\src\\main\\resources\\static\\leaveTemplate.pdf";//模板
        String outputFileName = "D:\\template6_out.pdf";//生成文件


//        // 填充表单的数据(文本)
//        Map<String, Object> data = new HashMap<String, Object>();
//        data.put("groundProtection1", "合格");
//        data.put("groundProtection2", "不合格");
//        data.put("groundProtection3", "合格");
//        data.put("groundProtection4", "未涉及");
//        data.put("groundProtection5", "未涉及");
//        data.put("groundProtectionNotes", "今天不合格明天合格");
//
//        data.put("decorationProtection1", "合格");
//        data.put("decorationProtection2", "不合格");
//        data.put("decorationProtection3", "未涉及");
//        data.put("decorationProtection4", "合格");
//        data.put("decorationProtection5", "不合格");
//        data.put("decorationProtection6", "未涉及");
//        data.put("decorationProtection7", "未涉及");
//        data.put("decorationProtection8", "未涉及");

        OutputStream os = null;
        PdfStamper ps = null;
        PdfReader reader = null;
        try {
            // 1 获取文件的输出流
            os = new FileOutputStream(new File(outputFileName));
            // 2 读取pdf模板
            reader = new PdfReader(inputFileName);
            // 3 根据表单生成一个新的pdf
            ps = new PdfStamper(reader, os);
            // 4 获取pdf表单
            AcroFields form = ps.getAcroFields();
            // 5 给表单添加中文字体
//            BaseFont bf = BaseFont.createFont("Font/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//            form.addSubstitutionFont(bf);

            // 6遍历data赋值到form
            for (String key : hashmap.keySet()) {
                form.setField(key, hashmap.get(key).toString(),true);
            }
            ps.setFormFlattening(true);

            // 7 填充图片到form
//            PdfStamper stamper = ps;
//            picData.forEach((fieldName, imgSrc) -> {
//                try {
//                    List<AcroFields.FieldPosition> fieldPositions = form.getFieldPositions(fieldName);
//                    for (AcroFields.FieldPosition fieldPosition : fieldPositions) {
//                        // 通过域名获取所在页和坐标，左下角为起点
//                        int pageno = fieldPosition.page;
//                        Rectangle signrect = fieldPosition.position;
//                        float x = signrect.getLeft();
//                        float width = signrect.getWidth();
//                        float y = signrect.getBottom();
//                        // 读图片
//                        Image image = Image.getInstance(imgSrc);
//                        float img_width = image.getWidth();
//                        // 获取操作的页面
//                        PdfContentByte under = stamper.getOverContent(pageno);
//                        // 图片的大小
//                        image.scaleToFit(signrect.getWidth(), signrect.getHeight());
//                        // 添加图片
//                        image.setAbsolutePosition(x, y);
//                        under.addImage(image);
//                    }
//                } catch (BadElementException e) {
//                    e.printStackTrace();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                } catch (DocumentException e) {
//                    e.printStackTrace();
//                }
//            });

            log.info("生成pdf成功:{}", outputFileName);
        } catch (Exception e) {
            log.error("生成pdf失败:{}", outputFileName);
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                reader.close();
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
